package com.maple.phoneward.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.maple.phoneward.constants.Constants;
import com.maple.phoneward.utils.AppUtil;
import com.maple.phoneward.utils.Md5Util;
import com.maple.phoneward.utils.StringUtil;

/**
 * SettingActivity.java
 * 
 * @author YaJian Qiu
 * @time 2013-4-6 下午1:07:09<br>
 *       本类描述：防盗设置界面
 * 
 */
public class SettingActivity extends PreferenceActivity implements OnPreferenceChangeListener, OnPreferenceClickListener {

	boolean updateSetting = false;// 是否更新防盗密码

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.getPreferenceManager().setSharedPreferencesName(Constants.PREFERENCE_NAME);// 设置配置文件的名字
		this.addPreferencesFromResource(R.xml.setting);// 添加布局

		// 防盗密码
		Preference preference = findPreference(Constants.CONFIG_PHONEWARD_PASSWORD);
		// preference.setOnPreferenceChangeListener(this);
		preference.setOnPreferenceClickListener(this);
		// 开机锁定
//		preference = findPreference(Constants.IS_POWER_ON_LOCKED);
//		SharedPreferences powerOnPreference = preference.getSharedPreferences();
//		preference.setOnPreferenceChangeListener(this);
//		boolean isPowerOnLocked = powerOnPreference.getBoolean(Constants.IS_POWER_ON_LOCKED, false);
//		preference = findPreference(Constants.IS_SIM_CHANGE_LOCKED);
//		if (isPowerOnLocked) {
//			preference.setEnabled(false);
//		} else {
//			preference.setEnabled(true);
//		}
		// 安全号码1
		EditTextPreference safety1Preference = (EditTextPreference) findPreference(Constants.CONFIG_SAFETY_PHONE_NUMBER_1);
		safety1Preference.setOnPreferenceChangeListener(this);
		// safety1Preference.setOnPreferenceClickListener(this);
		SharedPreferences safety1Sp = safety1Preference.getSharedPreferences();
		String safety1Summary = safety1Sp.getString(Constants.CONFIG_SAFETY_PHONE_NUMBER_1, "");
		safety1Preference.setSummary(safety1Summary);
		// 安全号码2
		EditTextPreference safety2Preference = (EditTextPreference) findPreference(Constants.CONFIG_SAFETY_PHONE_NUMBER_2);
		safety2Preference.setOnPreferenceChangeListener(this);
		// safety2Preference.setOnPreferenceClickListener(this);
		SharedPreferences safety2Sp = safety2Preference.getSharedPreferences();
		String safety2Summary = safety2Sp.getString(Constants.CONFIG_SAFETY_PHONE_NUMBER_2, "");
		safety2Preference.setSummary(safety2Summary);
		// 备份邮箱
		EditTextPreference emailPreference = (EditTextPreference) findPreference(Constants.CONFIG_EMAIL);
		emailPreference.setOnPreferenceChangeListener(this);
		// emailPreference.setOnPreferenceClickListener(this);
		SharedPreferences emailSp = emailPreference.getSharedPreferences();
		String emailSummary = emailSp.getString(Constants.CONFIG_EMAIL, "");
		emailPreference.setSummary(emailSummary);

	}

	@Override
	public boolean onPreferenceChange(Preference preference, Object newValue) {
		String key = preference.getKey();
		String value = String.valueOf(newValue);
		if (StringUtil.equals(key, Constants.CONFIG_SAFETY_PHONE_NUMBER_1)) {
			if (!AppUtil.isPhoneNumber(value)) {
				AppUtil.showShortMessage(this, R.string.setting_safetynum_require_right_num);
				updateSetting = false;
			} else {
				String selfNum = AppUtil.getPhoneNumber(this);// 本机号码
				String safety2 = AppUtil.getFromSharedPreferences(this, Constants.CONFIG_SAFETY_PHONE_NUMBER_2);
				if (StringUtil.equals(selfNum, value)) {
					AppUtil.showShortMessage(this, R.string.step_2_not_self);
					updateSetting = false;
				} else if (StringUtil.equals(safety2, value)) {
					AppUtil.showShortMessage(this, R.string.step_2_not_equals);
					updateSetting = false;
				} else {
					preference.setSummary(value);
					updateSetting = true;
				}
			}
		} else if (StringUtil.equals(key, Constants.CONFIG_SAFETY_PHONE_NUMBER_2)) {
			if (!AppUtil.isPhoneNumber(value)) {
				AppUtil.showShortMessage(this, R.string.setting_safetynum_require_right_num);
				updateSetting = false;
			} else {
				String selfNum = AppUtil.getPhoneNumber(this);// 本机号码
				String safety1 = AppUtil.getFromSharedPreferences(this, Constants.CONFIG_SAFETY_PHONE_NUMBER_2);
				if (StringUtil.equals(selfNum, value)) {
					AppUtil.showShortMessage(this, R.string.step_2_not_self);
					updateSetting = false;
				} else if (StringUtil.equals(safety1, value)) {
					AppUtil.showShortMessage(this, R.string.step_2_not_equals);
					updateSetting = false;
				} else {
					preference.setSummary(value);
					updateSetting = true;
				}
			}
		} else if (StringUtil.equals(key, Constants.CONFIG_EMAIL)) {
			if (!AppUtil.isEmail(value)) {
				AppUtil.showShortMessage(this, R.string.step_3_toast);
				updateSetting = false;
			} else {
				preference.setSummary(value);
				updateSetting = true;
			}
		} else if (StringUtil.equals(key, Constants.IS_POWER_ON_LOCKED)) {
			if (StringUtil.isNotEmpty(value)) {
				boolean flag = Boolean.valueOf(value);
				Preference simPreference = findPreference(Constants.IS_SIM_CHANGE_LOCKED);
				if (flag) {
					simPreference.setEnabled(false);
				} else {
					simPreference.setEnabled(true);
				}
			}
			updateSetting = true;
		}
		return updateSetting;
	}

	@Override
	public boolean onPreferenceClick(Preference preference) {
		String key = preference.getKey();
		if (StringUtil.equals(key, Constants.CONFIG_PHONEWARD_PASSWORD)) {
			View view = LayoutInflater.from(this).inflate(R.layout.setting_change_password, null);
			final EditText oldEditText = (EditText) view.findViewById(R.id.setting_change_password_edit_text_old);
			final EditText newEditText = (EditText) view.findViewById(R.id.setting_change_password_edit_text_new);
			final EditText renewEditText = (EditText) view.findViewById(R.id.setting_change_password_edit_text_renew);
			new AlertDialog.Builder(this)//
					.setTitle(R.string.setting_password_layout_title)//
					.setView(view)//
					.setPositiveButton(R.string.ok, new OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							String oldPwd = Md5Util.md5(oldEditText.getText().toString());
							String newPwd = newEditText.getText().toString();
							String renewPwd = renewEditText.getText().toString();
							Context context = SettingActivity.this;
							String old = AppUtil.getFromSharedPreferences(context, Constants.CONFIG_PHONEWARD_PASSWORD);
							if (StringUtil.notEquals(oldPwd, old)) {
								AppUtil.showShortMessage(context, R.string.setting_password_wrong_old);
								updateSetting = false;// 不允许更新设置
								return;
							}
							if (!AppUtil.isPassword(newPwd)) {
								AppUtil.showShortMessage(context, R.string.step_1_input_toast);
								updateSetting = false;
								return;
							}
							if (StringUtil.isEmpty(renewPwd)) {
								AppUtil.showShortMessage(context, R.string.step_1_not_null2);
								updateSetting = false;
								return;
							}
							if (newPwd.length() < 6) {
								AppUtil.showShortMessage(context, R.string.step_1_lenght_6);
								updateSetting = false;
								return;
							}
							if (StringUtil.equals(newPwd, renewPwd)) {
								// 保存防盗密码
								AppUtil.saveToSharedPreferences(context, Constants.CONFIG_PHONEWARD_PASSWORD, newPwd);
								updateSetting = true;// 允许更新防盗密码
							} else {
								AppUtil.showShortMessage(context, R.string.step_1_not_equal);
								updateSetting = false;
								return;
							}
						}
					})//
					.setNegativeButton(R.string.cancel, null)//
					.create().show();
		}
		return updateSetting;
	}

}
